<template>
  <div class='mianshi'>
    <el-card>
    <el-form label-width="80px" :inline="true" ref="optionClear">
      <el-form-item label="关键字" class="form-input">
        <el-input v-model="keyword" placeholder="根据文章标题搜索"></el-input>
      </el-form-item>
      <el-form-item label="状态" class="form-input">
        <el-select v-model="state" placeholder="请选择">
          <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="clearInput">
          清除
        </el-button>
        <el-button size="small" type="primary" @click="getArticleList()">
          搜索
        </el-button>
        <el-button type="success" icon="el-icon-edit" size="small" @click="openDialog('新增文章')">
          新增技巧
        </el-button>
      </el-form-item>
    </el-form>
    <el-alert :closable="false" type="info" show-icon>
      <span slot="title">数据一共{{ total }}条</span>
    </el-alert>
    <el-table class="tableTab" :data="articleList" style="width: 100%;">
      <el-table-column
      label="序号"
      type="index"
      width="80"
      >
      </el-table-column>
      <el-table-column
      prop="title"
      label="文章标题"
      width="400">
        <template slot-scope="scope">
          {{scope.row.title}}
        <i @click="openVideo(scope.row.videoURL)" v-if="scope.row.videoURL" class="el-icon-film"></i>
        </template>
      </el-table-column>
      <el-table-column
      prop="visits"
      label="阅读数"
      width="173">
      </el-table-column>
      <el-table-column
      prop="username"
      label="录入人"
      width="172">
      </el-table-column>
      <el-table-column
      label="录入时间"
      width="172">
        <template slot-scope="scope">
          {{ scope.row.createTime | parseTimeByString }}
        </template>
      </el-table-column>
      <el-table-column
      prop="state"
      label="状态"
      width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">已启用</span>
          <span v-if="scope.row.state == 0">已禁用</span>
        </template>
      </el-table-column>
      <el-table-column
      label="操作">
      <template slot-scope="scope">
        <el-button type="text" @click="yuLan(scope.row)">预览</el-button>
        <el-button type="text" @click="jinYong(scope.row)">
          <span v-if="scope.row.state == 1">禁用</span>
          <span v-if="scope.row.state == 0">启用</span>
        </el-button>
        <el-button type="text" @click="updateArticle(scope.row, '修改文章')" :disabled="scope.row.state == 0">修改</el-button>
        <el-button type="text" @click="removeArticle(scope.row)" :disabled="scope.row.state == 0">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
      <el-row class="fenye">
        <el-pagination
          style="margin-top: 20px;text-align: right"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page" 
          :page-sizes="[3, 5, 10, 40]"
          :page-size="pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
      </el-row>
      <el-row>
        <el-dialog
          v-if="dialogVisible"
          :title="title"
          :visible.sync="dialogVisible"
          width="50%">
          <el-form label-width="100px" class="demo-ruleForm" :model="tanChu" :rules="rules">
            <el-form-item label="文章标题:" prop="title">
              <el-input v-model="tanChu.title"></el-input>
            </el-form-item>
            <el-form-item label="文章内容:" prop="articleBody">
              <quill-editor ref="myQuillEditor" v-hljs v-model="tanChu.articleBody" :options="editorOption"
              style="width:100%;height:200px;marginBottom:50px" />
            </el-form-item>
            <el-form-item label="视频地址:">
              <el-input v-model="tanChu.videoURL"></el-input>
            </el-form-item>
          </el-form>  
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addArticle">确 认</el-button>
          </span>
        </el-dialog>
      </el-row>
      <div class="videoDialog" v-if="showVideo">
        <i class="el-icon-close" circle @click="showVideo = false"></i>
        <video :src="heihei" class="lookVideo" autoplay=“autoplay”></video>
      </div>
      <el-row>
        <el-dialog
          v-if="yuLanDialog"
          title="预览文章"
          :visible.sync="yuLanDialog"
          width="60%">
          <article-preview :receive = RandomslistId></article-preview>
        </el-dialog>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import hljs from 'highlight.js'
import 'highlight.js/styles/tomorrow.css'
import ArticlePreview from '../components/articles-preview'
import { list, add, remove, update, detail, changeState } from '@/api/hmmm/articles.js'
export default {
  components: {
    ArticlePreview
  },
  data () {
    return {
      options: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      title: '',
      total: 0,
      page: 1,
      pagesize: 5,
      keyword: '',
      state: null,
      articleList: [],
      dialogVisible: false,
      shiPinUrl: null,
      showVideo: false,
      heihei: '',
      yuLanDialog: false,
      RandomslistId: {},
      jinYongData: {
        id: null,
        state: null
      },
      tanChu: {
        title: null,
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
        ]
      },
      editorOption: {
        // 富文本编辑器配置
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"], //加粗，斜体，下划线，删除线
            [{ list: "ordered" }, { list: "bullet" }], //列表
            ["code-block", "blockquote"], //引用，代码块
            ["image", "link"], //上传图片、上传视频
          ],
          syntax: {
            highlight: (text) => hljs.highlightAuto(text).value,
          },
        },
        theme: "snow",
      }
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const { data } = await list({page: this.page, pagesize:this.pagesize, keyword: this.keyword, state: this.state})
      this.articleList = data.items
      this.total = data.counts
      console.log(this.articleList)
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.getArticleList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getArticleList()
    },
    clearInput () {
      this.keyword = ''
      this.state = null
      this.getArticleList()
    },
    openDialog (val) {
      this.title = val
      this.dialogVisible = true
    },
    async addArticle () {
      try {
        const { data } = await add(this.tanChu)
        this.tanChu.title = null
        this.tanChu.articleBody = null
        this.tanChu.videoURL = null
        this.dialogVisible = false
        this.getArticleList()
        this.$message.success('操作成功')
      } catch (err) {
        this.$message.error('操作失败')
      }
    },
    async removeArticle (shanChu) {
      try {
        const { data } = await remove(shanChu)
        this.getArticleList()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message.error('删除成功')
      }
    },
    async updateArticle (xiuGai, val) {
      try {
        this.dialogVisible = true
        this.title = val
        const { data } = await update(xiuGai)
        this.tanChu.title = xiuGai.title
        this.tanChu.articleBody = xiuGai.articleBody
        this.tanChu.videoURL = xiuGai.videoURL
      } catch (err) {
        this.$message.error('操作失败')
      }
    },
    async jinYong (jinYong) {
      this.jinYongData.id = jinYong.id
      this.jinYongData.state = jinYong.state === 1 ? 0 : 1
      const { data } = await changeState(this.jinYongData)
      this.getArticleList()
    },
    openVideo (shiPinUrl) {
      this.showVideo = true
      this.heihei = shiPinUrl
    },
    async yuLan (yuLan) {
      this.yuLanDialog = true
      this.RandomslistId = yuLan
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.el-input{
  .el-input__inner{
    height: 32px;
  }
}
.el-button--success{
  margin-left: 450px;
}
.tableTab{
  margin-top: 15px;
}
.el-icon-film{
  color: blue;
}
.videoDialog{
  position: fixed;
  width: 700px;
  height: 400px;
  margin: 0 auto;
  top: 20%;
  left: 30%;
  .el-icon-close{
    font-size: 50px;
    margin-bottom: 20px;
    margin-left: 47%;
    background-color: #ccc;
    opacity: 0.5;
    color: #fff;
    padding: 5px;
    border-radius: 50%;
  }
}
.lookVideo{
  width: 100%;
}
</style>
